package com.example.neusoft215gdh.androidpro_21.Model;
import com.example.neusoft215gdh.androidpro_21.Interface.MasterFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.MasterQue;
import com.example.neusoft215gdh.androidpro_21.JavaBean.master;
import com.example.neusoft215gdh.androidpro_21.Listener.MasterDetailListener;
import com.example.neusoft215gdh.androidpro_21.Listener.MasterQueListener;
import com.example.neusoft215gdh.androidpro_21.Listener.masterListener;
import com.example.neusoft215gdh.androidpro_21.Service.MasterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by hp-pc on 2017/10/12.
 */
public class MasterModel implements MasterFace{
    private Retrofit retrofit;
    MasterService masterService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public MasterModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getMaster(String sessionId, int page,final masterListener listener) {
        masterService=retrofit.create(MasterService.class);
        Call<List<master>> call=masterService.getMaster(sessionId,page);
        call.enqueue(new Callback<List<master>>() {
            @Override
            public void onResponse(Call<List<master>> call, Response<List<master>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponseMaster(response.body());
            }
            @Override
            public void onFailure(Call<List<master>> call, Throwable t) {

            }
        });
    }

    @Override
    public void getDetailMaster(String id, final MasterDetailListener listener) {
        masterService=retrofit.create(MasterService.class);
        Call<master> call=masterService.getMasterById(id);
        call.enqueue(new Callback<master>() {
            @Override
            public void onResponse(Call<master> call, Response<master> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<master> call, Throwable t) {

            }
        });
    }

    @Override
    public void getMasterQue(String id,final MasterQueListener listener) {
        masterService=retrofit.create(MasterService.class);
        Call<List<MasterQue>> call=masterService.getMasterQue(id);
        call.enqueue(new Callback<List<MasterQue>>() {
            @Override
            public void onResponse(Call<List<MasterQue>> call, Response<List<MasterQue>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<MasterQue>> call, Throwable t) {

            }
        });
    }

    public Call userCollectMaster(int userid,String fan_id) {
        MasterService service = retrofit.create(MasterService.class);
        return service.userCollectMaster("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"2");
    }

    public Call isCollected(String fan_id,int userid){

        MasterService service = retrofit.create(MasterService.class);
        return service.isCollected(fan_id,userid,"2","lgj98rfkiqvenhrk2o7oe2po81");
    }


}
